--#include "data\config\equip\EquipSynthesisConfigEEx.lua" once
--[[
装备升阶
]]
function EquipTSArmyUpgrade( sysarg, equipPtr, equipIndex, useInherit, useAllEquip )
	if not equipPtr then
		return
	end
	--1-在身上，2-在英雄身上, 3-在背包里
	local exitFlag = Item.IsEquipInBodyOrBag( sysarg, equipPtr )
	if exitFlag == 0 then
		return
	end
	local isHero = 0
	if exitFlag == 2 then
		isHero = 1
	end

	local itemid = Item.getItemProperty( sysarg, equipPtr, Item.ipItemID, 0)
	--local isTheShenArmy = Item.getItemPropertyById( itemid, Item.ipItemTheShenArmy)
	--local primeEquip = Item.getItemPropertyById( itemid, Item.ipItemPrimeEquip)
	--local index = getEquipConfigIndexByFlags(isTheShenArmy, primeEquip)
	local index = equipIndex
	if not EquipSynthesisConfigEEx[index] then
		return
	end
	local cfg = EquipSynthesisConfigEEx[index][itemid]
	if not cfg then
		return
	end

	if not cfg.newEquipId or cfg.newEquipId <= 0 then
		return
	end

	local itemType 		= Item.getItemProperty( sysarg, equipPtr, Item.ipItemType, 0)
	local equipStar  	= Item.getItemProperty( sysarg, equipPtr, Item.ipItemStrong, 0)				--强化星级
	local lostStar		= Item.getItemProperty( sysarg, equipPtr, Item.ipItemLostStar, 0)			--损失星级
	local equipInject  	= Item.getItemProperty( sysarg, equipPtr, Item.ipItemInjectLevel, 0)		--注灵等级
	local equipLuck 	= Item.getItemProperty( sysarg, equipPtr, Item.ipItemLuck, 0)				--幸运（诅咒）
	local itemBind		= Item.getItemProperty( sysarg, equipPtr, Item.ipItemBind, 0)
	local initAttrValue = Item.getItemProperty( sysarg, equipPtr, Item.ipItemSpecSmith, 0)

	--先加到背包再装备上，所以要检查背包格子数
	local gridCount = Item.getBagEmptyGridCount(sysarg)
	if gridCount < 1 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0015, ttFlyTip)
		return
	end

	--升阶的消耗
	if not cfg.equipConsumes and not cfg.materialConsumes and not cfg.inheritConsumes then
		return
	end
	local mergeEquipConsumes

	if cfg.equipConsumes then
	   mergeEquipConsumes = MergeAward2(cfg.equipConsumes)
	end
	if exitFlag == 3 then
		local needCircle = Item.getItemPropertyById( cfg.newEquipId, Item.ipItemNeedCircle) or 0
		local myCircle = Actor.getIntProperty(sysarg ,PROP_ACTOR_CIRCLE)
		if myCircle < needCircle then
			Actor.sendTipmsg( sysarg, string.format(Lang.ScriptTips.EquipTheShenArmy002, needCircle), ttFlyTip )
			return
		end
		mergeEquipConsumes = MergeAward(mergeEquipConsumes,{{type =0, id = itemid, count=1}}) 
	elseif not Item.canEquipTakeOn( sysarg, cfg.newEquipId, isHero ) then 		--不满足升级条件
		local needCircle = Item.getItemPropertyById( cfg.newEquipId, Item.ipItemNeedCircle) or 0
		local needLevel  = Item.getItemPropertyById( cfg.newEquipId, Item.ipItemActorLevel) or 0
		if needCircle <= 0 then  		--不要求转数，只要求等级
			local descStr = Lang.ScriptTips.EquipTheShenArmy001
			if isHero then
				descStr = Lang.ScriptTips.EquipTheShenArmy008
			end
			Actor.sendTipmsg( sysarg, string.format(descStr, needLevel), ttFlyTip )
		elseif needLevel <= 1 then 		--要求转数，不要求等级
			local descStr = Lang.ScriptTips.EquipTheShenArmy002
			if isHero then
				descStr = Lang.ScriptTips.EquipTheShenArmy009
			end
			Actor.sendTipmsg( sysarg, string.format(descStr, needCircle), ttFlyTip )
		else 	
			local descStr = Lang.ScriptTips.EquipTheShenArmy003
			if isHero then
				descStr = Lang.ScriptTips.EquipTheShenArmy010
			end						--要求转数+等级
			Actor.sendTipmsg( sysarg, string.format(descStr, needCircle, needLevel), ttFlyTip )
		end
		return
	end

	if mergeEquipConsumes and not CheckConsumeCond( sysarg,  mergeEquipConsumes) then
		return 
	end

	if cfg.materialConsumes and not CheckConsumeCond( sysarg, cfg.materialConsumes ) then
		return 
	end

	if useInherit == 1 and cfg.inheritConsumes and not CheckConsumeCond( sysarg, cfg.inheritConsumes ) then
		return 
	end

	local newBindFlag = itemBind			
	if Item.isBasicEquip( itemType ) then		--基础装备升阶后强制为非绑
		newBindFlag = 0
	end
	local newEquipPtr = Item.createItem(cfg.newEquipId, 1, 0, 0, newBindFlag)			--创建装备实体，但此时尚未进入玩家背包
	if not newEquipPtr then
		return
	end

	if cfg.materialConsumes and not DoConsumeCond( sysarg, cfg.materialConsumes, 1, GameLog.clEquipTSArmyUpgradeFee, Lang.ScriptTips.EquipLog022 ) then
		return
	end
	if useInherit == 1 and cfg.inheritConsumes and not DoConsumeCond( sysarg, cfg.inheritConsumes, 1, GameLog.clEquipTSArmyUpgradeFee, Lang.ScriptTips.EquipLog022 ) then
		return
	end

	if useInherit == 1 then
		Item.transSmithAttrs( equipPtr, newEquipPtr )			--进行洗练属性转移，此时原装备必须存在
	end
	
	--删掉原装备，先删身上，没有再删背包
	if Item.removeEquip( sysarg, equipPtr, Lang.ScriptTips.EquipLog024, GameLog.clEquipTSArmyUpgradeDelOld ) < 1 then
		if Item.removeItem( sysarg, equipPtr, 1, Lang.ScriptTips.EquipLog024, GameLog.clEquipTSArmyUpgradeDelInBag ) < 1 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.EquipTheShenArmy004, ttFlyTip)
			return
		end
	end
	
	--副装备消耗可能跟主装备同一id，先处理主装备，再消耗。
	if cfg.equipConsumes and not DoConsumeCond( sysarg, MergeAward2(cfg.equipConsumes), 1, GameLog.clEquipTSArmyUpgradeFee, Lang.ScriptTips.EquipLog022 ) then
		return 
	end

	--为安全起见，先删除原装备，再把newEquipPtr加入玩家背包
	local addCount = Item.addItem( sysarg, newEquipPtr, Lang.ScriptTips.EquipLog023, GameLog.clEquipTSArmyUpgradeAdd, 1)
	if addCount < 1 then
		return
	end

	local isNewTheShenArmy = Item.getItemPropertyById( cfg.newEquipId, Item.ipItemTheShenArmy)
	local newPrimeEquip = Item.getItemPropertyById( cfg.newEquipId, Item.ipItemPrimeEquip)
	local newIndex = getEquipConfigIndexByFlags(isNewTheShenArmy, newPrimeEquip)

	--装备在身上或背包里，才能设置属性
	if useInherit == 1 then
		Item.setItemProperty( sysarg, newEquipPtr, Item.ipItemStrong, equipStar)
		Item.setItemProperty( sysarg, newEquipPtr, Item.ipItemLostStar, lostStar) 
		Item.setItemProperty( sysarg, newEquipPtr, Item.ipItemInjectLevel, equipInject) 
		Item.setItemProperty( sysarg, newEquipPtr, Item.ipItemLuck, equipLuck)
		if newIndex == 1 then
			Item.modifyItemProperty(newEquipPtr, Item.ipItemSpecSmith, initAttrValue)
		end
	else
		local newEquipStar = equipStar - 1
		if newEquipStar < 0 then
			newEquipStar = 0
		end
		local newEquipInjectStar = equipInject - 1
		if newEquipInjectStar < 0 then
			newEquipInjectStar = 0
		end
		local newEquipLuck = equipLuck
		if equipLuck > 0 then
			newEquipLuck = equipLuck - 1
		end
		Item.setItemProperty( sysarg, newEquipPtr, Item.ipItemStrong, newEquipStar )
		Item.setItemProperty( sysarg, newEquipPtr, Item.ipItemLostStar, lostStar) 
		Item.setItemProperty( sysarg, newEquipPtr, Item.ipItemInjectLevel, newEquipInjectStar) 
		Item.setItemProperty( sysarg, newEquipPtr, Item.ipItemLuck, newEquipLuck)
		--[[
		if index == 2 or index == 3 then
			Item.modifyItemProperty(newEquipPtr, Item.ipItemSpecSmith, initAttrValue)
		end
		]]
	end
	--[[
	if newIndex == 2 or newIndex == 3 then
		local newInitAttrValue = Item.getItemPropertyById( cfg.newEquipId, Item.ipItemInitSmith)
		Item.modifyItemProperty(newEquipPtr, Item.ipItemSpecSmith, newInitAttrValue)
	end
	]]

	if exitFlag == 1 or exitFlag == 2 then		--目标装备穿在身上，则升阶后新装备也要穿在身上
		Item.equipItem( sysarg, newEquipPtr, isHero )			--原装备是穿在身上的，新装备也穿到身上
	end

	local actorName = Actor.getName( sysarg )
	local actorId 	= Actor.getActorId( sysarg )
	local newItemid = Item.getItemProperty( sysarg, newEquipPtr, Item.ipItemID, 0)
	System.sendCommonLog( GameLog.clEquipTSArmyUpgrade, actorId, actorName, 0, newItemid, itemid, "", "", Lang.ScriptTips.EquipLog025 )
	SendEquipTheShenArmyResult(sysarg, index, 1, 1)		--操作成功

	if equipIndex ~= 1 then
		local itemLink = Item.getItemLinkMsg( newItemid, newEquipPtr )
		local msg = string.format( Lang.ScriptTips.EquipTheShenArmy006, Actor.getName( sysarg ), itemLink )
		System.broadcastTipmsg( msg, ttScreenCenter + ttChatWindow )
	end

	Actor.triggerQuestEvent(sysarg, 48, 3, 1)
	Actor.triggerAchieveEvent(sysarg, 105, 1)
end

--[[
装备回收
]]
function EquipTheShenArmyRecover(sysarg, args, btRecoveryType)
	local nIdx 		= args[2] or 0			-- 等级索引
	local itemCount = args[4]				-- 装备数量
	print("EquipTheShenArmyRecover itemCount="..itemCount)
	local pos		= args[5]				-- 位置：0背包1寻宝仓库
	local packet 	= args[6]				-- 装备guid列表
	local equipItemPtr 	= {}   --存储物品指针用来最后删除
	local equipGuidList = {}  --把取出的每个物品的GUID储存,用来检测是否已存在一样的,有则是通过非法手段来进行
	local awardList = {}

	local allShieldValue 	= 0			--神盾值
	local allGodCastValue 	= 0			--神铸碎片

	local totalAwards = {}
	local index
	local funList =
	{
		[0] =
		{
			getBagItemPtrByGuid = Item.getBagItemPtrByGuid,
			removeItemByPtr		= Actor.removeItemByPtr,
		},
		[1] =
		{
		    getBagItemPtrByGuid = Dmkj.getItemPtrByGuid,
			removeItemByPtr		= Dmkj.removeItemByPtr,
		}
	}

	for i=1, itemCount do
		local itemGuid = DataPack.readDouble(packet)
		if not CheckItemGuidSame( equipGuidList, itemGuid ) then 
			return 
		end
		local itemPtr = funList[pos].getBagItemPtrByGuid( sysarg, itemGuid )
		if not itemPtr then
			return
		end
		
		-- 获取装备是否可回收属性
		local itemId 		= Item.getItemId(itemPtr)
		
		local isTheShenArmy = Item.getItemPropertyById( itemId, Item.ipItemTheShenArmy)
		local primeEquip = Item.getItemPropertyById( itemId, Item.ipItemPrimeEquip)
		local curIndex = getEquipConfigIndexByFlags(isTheShenArmy, primeEquip)
		if not index then
			index = curIndex
		elseif index ~= curIndex then
			return
		end
		if not EquipSynthesisConfigEEx[index] then
			return
		end
		local cfg = EquipSynthesisConfigEEx[index][itemId]
		if not cfg then
			return
		end

		if not cfg.recycleAwards then
			return
		end

		
		if not totalAwards or #totalAwards == 0 then
			totalAwards = DeepCopyFromObject( cfg.recycleAwards )    		--深度拷贝
		else
			totalAwards = AddAwardsToAwards( cfg.recycleAwards, totalAwards )
		end
		
		-- 保存装备指针
		table.insert( equipGuidList, itemGuid )
		table.insert( equipItemPtr, itemPtr)
	end

	if not index then
		return
	end

	if not totalAwards or #totalAwards == 0 then
		return
	end

	if not IsBagGridEnough( sysarg, totalAwards ) then
		Actor.sendTipmsg( sysarg, Lang.ScriptTips.EquipTheShenArmy005, ttFlyTip )
		return
	end
	
	local recoveryCount = 0
	-- 删除要回收的装备
	local nResult = true
	for _, itemPtr in ipairs( equipItemPtr or {}) do
		local itemId = Item.getItemId(itemPtr)
		if funList[pos].removeItemByPtr(sysarg, itemPtr, 1, true, Lang.ScriptTips.EquipLog026, GameLog.clEquipTSArmyRecycleDel) ~= 1 then
			nResult = false
			break
		end
		recoveryCount = recoveryCount + 1
	end
	
	if not nResult then			--必须全部装备删除成功
		return
	end

	--获得奖励
	GiveCommonAward(sysarg, totalAwards, GameLog.clEquipTSArmyRecycleAdd, Lang.ScriptTips.EquipLog027)
	SendEquipTheShenArmyResult(sysarg, index, 2, 1)		--操作成功

	local actorName = Actor.getName( sysarg )
	local actorId 	= Actor.getActorId( sysarg )
	System.sendCommonLog( GameLog.clEquipTSArmyRecycle, actorId, actorName, itemCount, allGodCastValue, allShieldValue, tostring(nIdx),tostring(pos), 
		Lang.ScriptTips.EquipLog028 )

	--成就事件
	Actor.triggerAchieveEvent(sysarg, 45, 1, recoveryCount)
end

function getEquipConfigIndexByFlags(isTheShenArmy, primeEquip)
	if isTheShenArmy == 1 then
		return 4
	elseif primeEquip == 1 then
		return 3
	end
	return 1
end

--神铸装备结果
function SendEquipTheShenArmyResult(sysarg, index, opType, result)
	local netPack = DataPack.allocPacket(sysarg, enEquipSystemID, sTheShenArmyCastResult)
	if netPack then
		DataPack.writeChar(netPack,index)
		DataPack.writeChar(netPack,opType)
		DataPack.writeChar(netPack,result)
		DataPack.flush(netPack)
		--print("SendEquipTheShenArmyResult, opType="..opType..", id="..result)
	end
end
